﻿#define TEST

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm
{
    public partial class Form1 : Form
    {
        private Utility utl = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = saveFileDialog1.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button3.Enabled = false;
            button4.Enabled = true;

            Initialize();
            MakeBuffer();
            utl.ReleaseMemory();

            button3.Enabled = true;
            button4.Enabled = false;
        }

        private void Initialize()
        {
            Utility.datasourceAlias = "route"; //textBox1.Text.Substring(textBox1.Text.IndexOf(".sdb"));
            utl = new Utility(axSuperWorkspace1, textBox1.Text);
        }

        private void MakeBuffer()
        {
            soDataset objDS = null;
            soStrings objFieldStr = null;

            objFieldStr = new soStrings();
            objFieldStr.Add(Route.FEATURE_CD);
            objFieldStr.Add(Route.DAIBUN_CD);
            objFieldStr.Add(Route.TSUUSHO_CD);

            for (int i = 1; i <= utl.objSource.Datasets.Count; i++)
            {
                objDS = utl.objSource.Datasets[i];
                if (!objDS.Name.Contains(Utility.BUFFER_TITLE))
                {
                    MakeRoute(objDS as soDatasetVector, objFieldStr);
                    MakeBufferExe(objDS as soDatasetVector, objDS.Name);
                }
            }

            Marshal.ReleaseComObject(objFieldStr);
            objFieldStr = null;
            Marshal.ReleaseComObject(objDS);
            objDS = null;
        }

        private void MakeRoute(soDatasetVector objDSV, soStrings objFieldStr)
        {
            soRecordset objRS = null;

            objRS = objDSV.Query("-1 < 0", false, objFieldStr, 
                "group by " + Route.FEATURE_CD + ", " + Route.DAIBUN_CD + ", " + Route.TSUUSHO_CD);

            if (objRS != null && !(objRS.IsBOF() && objRS.IsEOF()))
            {
                if (!objRS.IsBOF())
                {
                    objRS.MoveFirst();
                }
                IRoute route = new Route();
#if TEST
                for (int j = 0; j < objRS.RecordCount; objRS.MoveNext(), j++)
#else
                for (; !objRS.IsEOF(); objRS.MoveNext())
#endif
                {
                    route.MakePartsForRoute(objDSV, objRS);
                }

                Marshal.ReleaseComObject(objRS);
                objRS = null;
            }
        }

        private void MakeBufferExe(soDatasetVector objDSV, string dsName)
        {
            soGeoLine objLine = null;
            soGeoRegion objRegion = null;
            soRecordset objRS = null;

            objRS = objDSV.Query("-1 < 0", true, null, "");
            if (objRS != null && !(objRS.IsBOF() && objRS.IsEOF()))
            {
                if (!objRS.IsBOF())
                {
                    objRS.MoveFirst();
                }
#if TEST
                for (int j = 0; j < objRS.RecordCount; objRS.MoveNext(), j++)
#else
                for (; !objRS.IsEOF(); objRS.MoveNext())
#endif
                {
                    objLine = (soGeoLine)objRS.GetGeometry();
                    objRegion = objLine.Buffer(Utility.BUFFER_ALARM_DISTANCE, Utility.BUFFER_ALARM_SMOOTH);
                    Utility.routeFtWithLn[objRS.GetFieldValue(Utility.ROUTE_FIELD)] = objRegion;
                }

                objRS.MoveFirst();
                BuildBuffer(objRS, dsName + Utility.BUFFER_TITLE);

                Marshal.ReleaseComObject(objRegion);
                objRegion = null;
                Marshal.ReleaseComObject(objLine);
                objLine = null;
                Marshal.ReleaseComObject(objRS);
                objRS = null;
            }
        }

        private void BuildBuffer(soRecordset objRS, string datasetName)
        {
            soDatasetVector objDatasetV = null;
            soRecordset objRecords = null;

            if (!utl.objSource.IsAvailableDatasetName(datasetName))
            {
                utl.objSource.DeleteDataset(datasetName);
            }
            objDatasetV = (soDatasetVector)utl.objSource.CreateDataset(
                datasetName, seDatasetType.scdRegion, seDatasetOption.scoDefault, null);
            objRecords = objDatasetV.Query("-1 < 0", true, null, "");

            int i = 0, j = 0;
#if TEST
            for (; j < objRS.RecordCount; objRS.MoveNext())
#else
            for (; !objRS.IsEOF(); objRS.MoveNext())
#endif
            {
                i = objRecords.AddNew(
                    Utility.routeFtWithLn[objRS.GetFieldValue(Utility.ROUTE_FIELD)] as soGeometry,
                    false);
                Console.WriteLine("{0}, {1}", ++j, i);
                objRecords.Update();
            }

            Marshal.ReleaseComObject(objRecords);
            objRecords = null;
            Marshal.ReleaseComObject(objDatasetV);
            objDatasetV = null;
        }
    }
}